package org.zk.dubbo_provider.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.zk.dubbo_provider.dao.BroadcastMapper;
import org.zk.entity.Broadcast;
import org.zk.entity.Course;
import org.zk.entity.Teacher;
import org.zk.service.BroadcastService;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@Service(timeout = 10000)
@CacheConfig(cacheNames = "broadcast")
public class BroadcastServiceImpl  implements BroadcastService {

    @Resource
    private BroadcastMapper broadcastMapper;


    @Override
    public int insertBroadcast(Broadcast broadcast) {
        /*判断添加直播的时间是否合理*/
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date end = null;
        Date begin = null;

        System.out.println(broadcast.getBroadcastEndtime());
        System.out.println(broadcast.getBroadcastBegtime());

        try {
            end = df.parse(broadcast.getBroadcastEndtime());
            begin = df.parse(broadcast.getBroadcastBegtime());
            } catch (ParseException e) {
            e.printStackTrace();
        }

        if (begin.getTime() < end.getTime()) {
            return broadcastMapper.addBroadcast(broadcast);
        } else {
            return 0;
        }

    }

    /**
     * 查询老师有关的直播课程
     * @param map
     * @return
     */
    @Override
    public PageInfo getBroadcastByIdByPage(Map map) {
        PageInfo page=new PageInfo();
        if (map.get("pageSize") != null){
            int count=getBroadcastCountByIdByPageint(map);
            page.setTotal(count);
            int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
            page.setPageSize(pageSize);
            int currPage = Integer.parseInt(String.valueOf(map.get("pageNum")));
            page.setPageNum(currPage);

            //计算总页数
            int a ;
            if (count % pageSize > 0) {
                a = count / pageSize + 1;
            } else {
                a = count / pageSize;
            }
            page.setPages(a);

            //开始分页
            PageHelper.startPage(currPage,pageSize);
            List<Course> ls= broadcastMapper.getBroadcastByIdByPage(map);
            page.setList(ls);
        }else {
            List<Course> ls= broadcastMapper.getBroadcastByIdByPage(map);
            page.setList(ls);
        }

        return page;
    }

    @Override
    public int getBroadcastCountByIdByPageint(Map<String, Object> map) {
        return broadcastMapper.getBroadcastCountByIdByPage(map);
    }

    @Override
    public int deleteBroadcastById(Broadcast broadcast) {
        return broadcastMapper.deleteBroadcastById(broadcast);
    }

    @Override
    public int updateBroadcast(Broadcast broadcast) {
        return broadcastMapper.updateBroadcast(broadcast);
    }

    /**
     * @MethodName: getBroadcastAndTeacherByBroadcastId
     * @Description: 根据直播的id查询直播和老师信息
     * @Param: [broadcastId]
     * @Return: org.zk.entity.Broadcast
     * @Author: 刘艺广
     * @Time: 2019/12/25 0025 下午 8:18
     **/
    @Override
    public Broadcast getBroadcastAndTeacherByBroadcastId(int broadcastId) {
        return broadcastMapper.getBroadcastAndTeacherByBroadcastId(broadcastId);
    }

    @Override
    public PageInfo getAllBroadcastPage(Map<String, Object> map) {
        PageInfo page=new PageInfo();

        int count=broadcastMapper.getBroadcastCount(map);
        page.setTotal(count);
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);
        int currPage = Integer.parseInt(String.valueOf(map.get("pageNum")));
        page.setPageNum(currPage);

        //计算总页数
        int a ;
        if (count % pageSize > 0) {
            a = count / pageSize + 1;
        } else {
            a = count / pageSize;
        }
        page.setPages(a);

        //开始分页
        PageHelper.startPage(currPage,pageSize);
        List<Broadcast> ls= broadcastMapper.getAllList(map);
        page.setList(ls);
        return page;
    }

    @Override
    public int updateFlag(Broadcast broadcast) {
        return broadcastMapper.updateFlag(broadcast);
    }

    @Override
    public List<Broadcast> getBraodcastBeingByTypeId(Map map) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String currTime = df.format(new Date());
        map.put("currTime",currTime);
        System.out.println(map.get("typeId"));
        System.out.println(map.get("currTime"));
        return broadcastMapper.getBraodcastBeingByTypeId(map);
    }
    /**
     * @MethodName: getAllBroadcastList
     * @Description: 综合分页查询所有直播课程
     * @Param: [map]
     * @Return: java.util.List<org.zk.entity.Broadcast>
     * @Author: 刘艺广
     * @Time: 2020/1/7 0007 上午 11:08
     **/
    @Override
    public List getAllBroadcastList(Map map) {
        List<Broadcast> broadcastList= broadcastMapper.getAllBroadcastList(map);
        return broadcastList;
    }

    @Override
    public Broadcast getBroadcastById(int broadcastId) {
        return broadcastMapper.getBroadcastById(broadcastId);
    }

    @Override
    public void updateBroadcastCount(Broadcast broadcast) {
        broadcastMapper.updateBroadcastCount(broadcast);
    }

}
